package org.example.baozi;

public class Consume implements Runnable{
    private BaoZiPu bzp;

    public Consume(BaoZiPu bzp) {
        this.bzp = bzp;
    }

    @Override
    public void run() {
        while(true){
            synchronized (bzp) {
                if (bzp.isFlag()) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //生产完包子了就给包子设置编号
                    System.out.println("消费者消费了包子：" + bzp.getBaoZiId());
                    //需要给包子铺的标记设置为真
                    bzp.setFlag(false);
                    //叫醒生产者
                    bzp.notify();
                } else {
                    try {
                        bzp.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
